package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.ironhide.dto.param.manager.L2RServicesParamDto;
import com.link2room.ironhide.dto.result.manager.L2RServiceDto;
import com.link2room.ironhide.entity.L2RGroupService;
import com.link2room.ironhide.entity.L2RHotelService;
import com.link2room.ironhide.facade.IL2RServicesFacadeService;
import com.link2room.ironhide.service.IL2RServicesService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by sss on 2017/6/28.
 */
@Component("groupServicesFacadeService")
public class L2RServicesFacadeServiceImpl implements IL2RServicesFacadeService {
    @Resource
    private IL2RServicesService l2RServicesService;

    @Override
    public List<L2RServiceDto> listGroupServices(BaseParamDto baseParamDto)throws Exception{
        L2RGroupService queryGS=new L2RGroupService();
        queryGS.setLhotelGroupCode(baseParamDto.getLhotelGroupCode());
        List<L2RServiceDto> l2RServiceDtos = l2RServicesService.listL2RServices(baseParamDto.getLhotelGroupCode(), null, queryGS, L2RGroupService.class,false);
        return l2RServiceDtos;
    }

    @Override
    public void saveOrUpdateGroupServices(L2RServicesParamDto l2RServicesParamDto)throws Exception{
        List<L2RServiceDto> l2RServiceDtos=this.getAllL2RServiceDto(null,l2RServicesParamDto.getL2RServiceDtos(),false);
        Iterator<L2RServiceDto> l2RServiceDtoIterator = l2RServiceDtos.iterator();
        List<Long> delIds=new ArrayList<>();
        while (l2RServiceDtoIterator.hasNext()){
            L2RServiceDto l2RServiceDto = l2RServiceDtoIterator.next();
            if(DefaultValue.T.equals(l2RServiceDto.getIsHalt())&&l2RServiceDto.getId()!=null){
                delIds.add(l2RServiceDto.getId());
                l2RServiceDtoIterator.remove();
            }
        }
        l2RServicesService.saveOrUpdateL2RServiceDto(delIds.toArray(),l2RServiceDtos);
    }

    @Override
    public List<L2RServiceDto> listL2RHotelServicesUse(BaseParamDto baseParamDto)throws Exception{
        L2RGroupService queryGS=new L2RGroupService();
        queryGS.setLhotelGroupCode(baseParamDto.getLhotelGroupCode());
        List<L2RServiceDto> groupServicesList = l2RServicesService.listL2RServices(baseParamDto.getLhotelGroupCode(), null, queryGS, L2RGroupService.class,false );

        L2RHotelService queryHS=new L2RHotelService();
        queryHS.setLhotelGroupCode(baseParamDto.getLhotelGroupCode());
        queryHS.setLhotelCode(baseParamDto.getLhotelCode());
        List<L2RServiceDto> hotelServicesList = l2RServicesService.listL2RServices(baseParamDto.getLhotelGroupCode(), baseParamDto.getLhotelCode(), queryHS, L2RHotelService.class,true );
        return this.getUseL2RServiceDto(groupServicesList,hotelServicesList);
    }

    /**
     * 保存或更新酒店设施服务
     * @param l2RServicesParamDto
     * @throws Exception
     */
    @Override
    public void saveOrUpdateL2RHotelServices(L2RServicesParamDto l2RServicesParamDto)throws Exception{
        if(l2RServicesParamDto.getL2RServiceDtos()!=null){
            List<L2RServiceDto> l2RServiceDtos= this.getAllL2RServiceDto(l2RServicesParamDto.getLhotelCode(),l2RServicesParamDto.getL2RServiceDtos(),true);
            if(l2RServiceDtos!=null&&l2RServiceDtos.size()>0){
                l2RServicesService.saveOrUpdateHotelServices(l2RServicesParamDto.getLhotelGroupCode(),
                        l2RServicesParamDto.getLhotelCode(),
                        ClassConverUtil.copyPropertiesList(l2RServiceDtos,L2RHotelService.class));
            }
        }
    }

    private List<L2RServiceDto> getAllL2RServiceDto(String lhotelCode,List<L2RServiceDto> l2RServiceDtoList,boolean isHotelUse){
        List<L2RServiceDto> l2RServiceDtos=new ArrayList<>();
        if(l2RServiceDtoList!=null){
            for(L2RServiceDto l2RServiceDto:l2RServiceDtoList){
                if(isHotelUse) {
                    l2RServiceDto.setLhotelCode(lhotelCode);
                }
                l2RServiceDto.setIsHalt(DefaultValue.T);
                if (DefaultValue.T.equals(l2RServiceDto.getIsUse())) {
                    l2RServiceDto.setIsHalt(DefaultValue.F);
                }
                l2RServiceDtos.add(l2RServiceDto);

                if(l2RServiceDto.getChildren()!=null&&l2RServiceDto.getChildren().size()>0){
                    l2RServiceDtos.addAll(this.getAllL2RServiceDto(lhotelCode,l2RServiceDto.getChildren(),isHotelUse));
                }
            }
        }
        return l2RServiceDtos;
    }


    private <S extends L2RServiceDto,T extends L2RServiceDto> List<S> getUseL2RServiceDto(List<S> l2rServiceList1,
                                                                                          List<T> l2rServiceList2)throws Exception{
        for(S p1:l2rServiceList1){
            for(T p2:l2rServiceList2){
                if(p1.getServiceType().equals(p2.getServiceType())&&p1.getServiceCode().equals(p2.getServiceCode())){
                    if(DefaultValue.F.equals(p2.getIsHalt())) {
                        p1.setIsUse("T");
                    }
                    if(p1.getChildren()!=null&&p1.getChildren().size()>0&&
                            p2.getChildren()!=null&&p2.getChildren().size()>0){
                        this.getUseL2RServiceDto(p1.getChildren(),p2.getChildren());
                    }
                }
            }
        }
        return l2rServiceList1;
    }
}
